<?php
namespace User\Controller;

use Zend\Mvc\Controller\AbstractActionController;

use Zend\View\Model\JsonModel;

use Zend\Form\Form;
use Zend\Form\Element;

class AuthController extends AbstractActionController
{
    protected $form;
    protected $storage;
    protected $authService;
    protected $userTable;
    
    public function getUserTable()
    {
        if(!$this->userTable)
        {
            $this->userTable = $this->getServiceLocator()->get('User\Model\UserTable');
            
        }
        return $this->userTable;
    }
    
    public function getAuthService()
    {
    	if(!$this->authService)
    	{
    		$this->authService = $this->getServiceLocator()->get('AuthService');
    	}
    	
    	return $this->authService;
    }
    
    public function getSessionStorage()
    {
    	if(!$this->storage)
    	{
    		$this->storage = $this->getServiceLocator()->get('User\Model\TtkAuthStorage');
    	}
    	return $this->storage;
    }
    
    public function isAuthenticatedAction()
    {
    	if($this->request->isXmlHttpRequest())
        {
            $value = '';
            if($this->getAuthService()->hasIdentity())
            {
                $value = 'true';
            }
            else
            {
                $value = 'false';
            }
            return new JsonModel(array(
                    'value' => $value
                ));
        }
    }
    
    public function indexAction()
    {
        if($this->getAuthService()->hasIdentity())
        {
            return array();
        }
        else
        {
            $this->redirect()->toRoute('user/process', array('controller'=>'auth', 'action' => 'login'));
        }
    }
    
    public function loginAction()
    {
        $username = new Element\Text('username');
        $username->setLabel('Username');
        
        $password = new Element\Password('password');
        $password->setLabel('Password');
        
        $submit = new Element\Submit('submit');
        $submit->setValue('Login');
        
        $form = new Form('loginform');
        
        $form->add($username)->add($password)->add($submit);
        
        return array('form' => $form);
    }

    public function authenticateAction()
    {
    	$request = $this->request;
        
        $this->getAuthService()
            ->getAdapter()
            ->setIdentity($request->getPost('username'))
            ->setCredential($request->getPost('password'));

        $result = $this->getAuthService()->authenticate();

        if($this->request->isXmlHttpRequest())
        {
            if($result->isValid())
            {

    //                if($request->getPost('rememberMe') == 1)
    //                {
    //                        $this->getSessionStorage()->setRememberMe(1);
    //                        $this->getAuthService()->setStorage($this->getSessionStorage());
    //                }

                $userInfo = $this->getAuthService()->getAdapter()->getResultRowObject(null, array('password'));
                $this->getAuthService()->getStorage()->write($userInfo);

                return new JsonModel(array(
                    'success'=>'true',
                    'user' => $userInfo
                ));
            }
            else
            {
                return new JsonModel(array(
                    'success'=>'false',
                    'msg' => 'Credential wrong'
                ));
            }
        }
        else 
        {
            if($result->isValid())
            {

    //                if($request->getPost('rememberMe') == 1)
    //                {
    //                        $this->getSessionStorage()->setRememberMe(1);
    //                        $this->getAuthService()->setStorage($this->getSessionStorage());
    //                }

                $userInfo = $this->getAuthService()->getAdapter()->getResultRowObject(null, array('password'));
                $this->getAuthService()->getStorage()->write($userInfo);  
            }
            
            $this->redirect()->toRoute('user/process', array('controller'=>'auth', 'action' => 'index'));
        }
    }
    
    public function logoutAction()
    {
    	$this->getSessionStorage()->forgetMe();
    	$this->getAuthService()->clearIdentity();
        
        if($this->request->isXmlHttpRequest())
        {
            return new JsonModel(array(
                'success'=>'true',
                'msg' => 'Logout successful!'
            ));
        }
    	else
        {
            $this->flashmessenger()->addMessage("You've been logged out");
            $this->redirect()->toRoute('user/process', array('controller'=>'auth', 'action' => 'login'));
        }
    }
    
    public function changePasswordAction()
    {
        $msg = '';
        
        if($this->request->isPost())
        {
            $pass = $this->params()->fromPost('password');
            if($this->getAuthService()->hasIdentity())
            {
                $userInfo = $this->getAuthService()->getIdentity();
                $uid = $userInfo->id;
                $this->getUserTable()->changePassword($uid, $pass);
                
                $msg = 'Your password has been changed!';
            }
            else
            {
                $msg = 'You have not logged in yet.';
            }
        }
        
        if($this->request->isXmlHttpRequest())
        {
            return new JsonModel(array(
                'success' => 'true',
                'msg' => $msg
            ));
        }
        else
        {
            return array('result' => $msg);
        }
        
        
    }
    	
}
